package com.mt.api.core.sms;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.mt.api.core.config.property.ConfigProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: plover
 * @Date: 2020/07/09/2:44 下午
 * @Description:
 */
@Slf4j
@Component
public class SmsUtils {

    private static ConfigProperties configProperties;

    @Autowired
    public void setConfigProperties(ConfigProperties configProperties) {
        SmsUtils.configProperties = configProperties;
    }

    public static void send(SmsSend smsSend) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", configProperties.getSms().getAccessKeyId(), configProperties.getSms().getAccessSecret());
        /** use STS Token
         DefaultProfile profile = DefaultProfile.getProfile(
         "<your-region-id>",           // The region ID
         "<your-access-key-id>",       // The AccessKey ID of the RAM account
         "<your-access-key-secret>",   // The AccessKey Secret of the RAM account
         "<your-sts-token>");          // STS Token
         **/
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("SignName", "梦树科技");
        request.putQueryParameter("TemplateCode", smsSend.getTemplateCode());
        request.putQueryParameter("PhoneNumbers", smsSend.getPhoneNumbers());
        request.putQueryParameter("TemplateParam", smsSend.getTemplateParam().toJSONString());
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
            log.error("短信发送失败,参数：{}", JSON.toJSONString(smsSend), e);
        } catch (ClientException e) {
            e.printStackTrace();
            log.error("短信发送失败,参数：{}", JSON.toJSONString(smsSend), e);
        }
    }

}
